package com.wms.crypt;

import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;

import javax.crypto.Cipher;

import org.apache.commons.lang3.ArrayUtils;
import org.springframework.stereotype.Service;

@Service
public class EncryptAndDEcrypt {
	
	private static final int ENCRYPTIONBUFFER = 102;
	private static final int DECRYPTIONBUFFER = 128;
	
	private static final String PUBLICKEY  = "classpath:Server_Rsa_PbKey.dat";   //server  加密
	private static final String PRIVATEKEY = "classpath:Browser_Rsa_PrKey.dat";  //browser 解密
	
	
	
	
	//加密,返回byte[]
	public byte[] encrypToByte(String content)throws Exception{
		
		//从文件中读取公钥
        ObjectInputStream objectPukey=new ObjectInputStream(new FileInputStream(PUBLICKEY));
        RSAPublicKey pbk=(RSAPublicKey)objectPukey.readObject();
        objectPukey.close();
		
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, pbk);
        
        byte[] contBytes = content.getBytes("UTF-8");
        
        byte[] resBytes = new byte[]{};
        
        for(int i=0; i<contBytes.length; i += ENCRYPTIONBUFFER){
        	
        	resBytes = ArrayUtils.addAll(resBytes, cipher.doFinal(ArrayUtils.subarray(contBytes,i,i+ENCRYPTIONBUFFER)));
        }
        
        return resBytes;
        
	}
	
	
	//加密,返回hex16进制string
	public String encrypToHex(String content)throws Exception{
		
		//从文件中读取公钥
        ObjectInputStream objectPukey=new ObjectInputStream(new FileInputStream(PUBLICKEY));
        RSAPublicKey pbk=(RSAPublicKey)objectPukey.readObject();
        objectPukey.close();
		
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, pbk);
        
        byte[] contBytes = content.getBytes("UTF-8");
        
        byte[] resBytes = new byte[]{};
        
        for(int i=0; i<contBytes.length; i += ENCRYPTIONBUFFER){
        	
        	resBytes = ArrayUtils.addAll(resBytes, cipher.doFinal(ArrayUtils.subarray(contBytes,i,i+ENCRYPTIONBUFFER)));
        }
        
        String hv;
		
		StringBuilder stringBuilder = new StringBuilder("");
		 
		for (int i = 0; i < resBytes.length; i++) {  
	          int v = resBytes[i] & 0xFF;  
	          hv = Integer.toHexString(v);  
	          if (hv.length() < 2) {  
	              stringBuilder.append(0);  
	          }  
	          stringBuilder.append(hv);
	    }  
	    return stringBuilder.toString(); 
        
	}
		
		
		
		
	//解密,返回 json string
	public String decrypToString(String ciphertext)throws Exception{
		
		//以下段落为对hex16进制string解析成byte
		ciphertext = ciphertext.toUpperCase();  
	    int length = ciphertext.length() / 2;  
	    char[] hexChars = ciphertext.toCharArray();  
	    byte[] content = new byte[length];  
	    int pos;
	    for (int i = 0; i < length; i++) {  
	        pos = i * 2;  
	        content[i] = (byte) ( "0123456789ABCDEF".indexOf(hexChars[pos]) << 4 |  "0123456789ABCDEF".indexOf(hexChars[pos + 1]));  
	    }  
        
        //获取私钥 
        ObjectInputStream objectPrkey = new ObjectInputStream(new FileInputStream(PRIVATEKEY));
        RSAPrivateKey prk =(RSAPrivateKey)objectPrkey.readObject();
        objectPrkey.close();
        
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, prk);
        
        byte[] resBytes = new byte[]{};
        
        for(int i=0; i<content.length; i += DECRYPTIONBUFFER){
        	resBytes = ArrayUtils.addAll(resBytes,cipher.doFinal(ArrayUtils.subarray(content,i,i+DECRYPTIONBUFFER)));
        }
        
        return new String(resBytes,0,resBytes.length,"UTF-8");
        
	}	
		
		
		
}
